package com.zkk.web;

import com.alibaba.fastjson.JSON;
import com.zkk.pojo.DailyRecord;
import com.zkk.pojo.Page;
import com.zkk.service.DailyRecordService;
import com.zkk.service.impl.DailyRecordServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/showRecord")
public class showRecordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");
        //先获取前端传进来的关键两个值，一个当前页，一个每页显示数量，如果没传值，就使用默认值
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        if(currentPage==null){
            currentPage="1";
        }
        if(pageSize==null){
            pageSize="5";
        }
        Page<DailyRecord> page = new Page<>();
        page.setCurrentPage(Integer.parseInt(currentPage));
        page.setPageSize(Integer.parseInt(pageSize));
        DailyRecordService dailyRecordService = new DailyRecordServiceImpl();
        Page<DailyRecord> pageData = dailyRecordService.showPage(page);
        req.setAttribute("pages",pageData);
        req.getRequestDispatcher("/pages/record/record.jsp").forward(req,resp);
        /*resp.sendRedirect("/pages/record/record.jsp");*/

    }
}
